/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package lineage.util;

/**
 * 处理'8位无符号byte'的转换.
 * 
 * @version 2014年3月8日下午2:06:54
 * @author jrwz
 */
public final class UByte8 {
    /**
     * 将一个32位(unsigned/signed)long型数组 转换为 8位无符号byte数组.
     * 
     * @param buff
     *            - 要转换的long数组
     * @return byte[] 一个8位无符号byte数组
     */
    public static byte[] fromArray(final long[] buff) {
        final byte[] byteBuff = new byte[buff.length * 4];

        for (int i = 0; i < buff.length; ++i) {
            byteBuff[(i * 4) + 0] = (byte) (buff[i] & 0xFF);
            byteBuff[(i * 4) + 1] = (byte) ((buff[i] >> 8) & 0xFF);
            byteBuff[(i * 4) + 2] = (byte) ((buff[i] >> 16) & 0xFF);
            byteBuff[(i * 4) + 3] = (byte) ((buff[i] >> 24) & 0xFF);
        }
        return byteBuff;
    }

    /**
     * 将一个8位无符号char数组 转换为 8位无符号byte数组.
     * 
     * @param buff
     *            - 要转换的char数组
     * @return byte[] 一个8位无符号byte数组
     */
    public static byte[] fromArray(final char[] buff) {
        final byte[] byteBuff = new byte[buff.length];

        for (int i = 0; i < buff.length; ++i) {
            byteBuff[i] = (byte) (buff[i] & 0xFF);
        }
        return byteBuff;
    }

    /**
     * 将一个8位无符号char 转换为 8位无符号byte.
     * 
     * @param c
     *            - 要转换的char
     * @return byte 一个8位无符号byte
     */
    public static byte fromUChar8(final char c) {
        return (byte) (c & 0xFF);
    }

    /**
     * 将一个32位无符号long 转换为 8位无符号byte.
     * 
     * @param l
     *            - 要转换的long
     * @return byte 一个8位无符号char
     */
    public static byte[] fromULong32(final long l) {
        final byte[] byteBuff = new byte[4];

        byteBuff[0] = (byte) (l & 0xFF);
        byteBuff[1] = (byte) ((l >> 8) & 0xFF);
        byteBuff[2] = (byte) ((l >> 16) & 0xFF);
        byteBuff[3] = (byte) ((l >> 24) & 0xFF);
        return byteBuff;
    }
}
